package com.baymax.test.scene.demo

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.ShapeDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.math.MathUtils
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.newScrollContainer
import kotlin.properties.Delegates

/**
 * @title:       标题
 * @project:     TestScene
 * @package:     com.baymax.test.scene.demo
 * @class:       CanvasScene
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2023/2/23 22:46
 * @Copyright (C) 2023 YSTEN
 * @author:       xuyujian
 */
class CanvasScene : BaseScene() {

    lateinit var v:View
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newScrollContainer {

            val paint = Paint(Paint.ANTI_ALIAS_FLAG)
            val paint2 = Paint(Paint.ANTI_ALIAS_FLAG)
            val paint3 = Paint(Paint.ANTI_ALIAS_FLAG)
            var textCalc = 0f
            var text  by Delegates.observable(24 ){ _ , o ,n ->
                println("displayMetrics ${p1.context.resources.displayMetrics}")
                println("displayMetrics ${p1.context.resources.displayMetrics.density}")
                textCalc = MathUtils.clamp(n.toFloat(), 12f, 25f) * (p1.context.resources.displayMetrics.density - 0.6f)
                paint.textSize = n.toFloat()
                paint2.textSize = n * p1.context.resources.displayMetrics.density
                paint3.textSize = textCalc * 2

                paint3.color = Color.RED
                paint2.color = Color.RED
                paint.color = Color.RED
                v.postInvalidate()
            }


            v = object : View(p1.context) {

                init {
                    background = GradientDrawable().apply {
                        setStroke(2 ,Color.BLUE)
                    }
                    setPadding(60 ,100 ,60 , 60)
                }

                override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
                    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
                    setMeasuredDimension( 600 ,600)

                }

                override fun onDraw(canvas: Canvas) {
                    super.onDraw(canvas)
                    //canvas?.restore()
                    canvas?.translate(100f ,100f)
                    canvas?.drawText("TEXT:$text" , 60f,  60f ,paint)
                    canvas?.drawText("TEXT2:$text" , 60f,  80f + paint.textSize ,paint2)
                    canvas?.drawText("TEXT3:$textCalc" , 60f,  160 + paint2.textSize ,paint3)
                    canvas?.save()
                }
            }
            addButton("++++") {
                text += 2
            }
            addButton("----") {
                text -= 2
            }
            addView(v)



        }
    }
}